unit RProperties;

interface

uses
  Database, RoomExit, RenameExit, ConfirmLink,
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, DB, Grids, Mask,
  ExtCtrls, Menus, ComCtrls, ColorGrd,
  DBTables, DBGrids, PortalGrid, SMDBGrid;

type
  TFormRProperties = class(TForm)
    BtnDelete: TBitBtn;
    PanelProperties: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label6: TLabel;
    EditLabel: TEdit;
    EditNotes: TMemo;
    EditX: TEdit;
    EditY: TEdit;
    EditMap: TEdit;
    BtnAdd: TBitBtn;
    BtnCancel: TBitBtn;
    BtnEdit: TBitBtn;
    Label5: TLabel;
    EditTag: TEdit;
    BtnRename: TBitBtn;
    Label7: TLabel;
    EditWarn: TEdit;
    BtnHelp: TBitBtn;
    MainMenu1: TMainMenu;
    Main1: TMenuItem;
    MenuHelp: TMenuItem;
    Image1: TImage;
    Image2: TImage;
    Image3: TImage;
    Image4: TImage;
    PanelRoom: TPanel;
    Bevel5: TBevel;
    ColorGrid: TColorGrid;
    PanelToolbar: TPanel;
    rrr0: TSpeedButton;
    rrr1: TSpeedButton;
    rrr2: TSpeedButton;
    rrr3: TSpeedButton;
    rrr4: TSpeedButton;
    BtnSave: TBitBtn;
    BoxStubs: TGroupBox;
    ImageNW: TImage;
    ImageSE: TImage;
    ImageSW: TImage;
    ImageNE: TImage;
    ImageN: TImage;
    ImageS: TImage;
    ImageW: TImage;
    ImageE: TImage;
    ShapeRoom: TShape;
    ImageStyle: TImage;
    LabelTag: TLabel;
    CheckNW: TCheckBox;
    CheckN: TCheckBox;
    CheckNE: TCheckBox;
    CheckW: TCheckBox;
    CheckE: TCheckBox;
    CheckSW: TCheckBox;
    CheckS: TCheckBox;
    CheckSE: TCheckBox;
    Check5: TCheckBox;
    Shape5: TShape;
    BtnClear1: TBitBtn;
    Panel1: TPanel;
    Status: TLabel;
    BtnJump: TBitBtn;
    rrr5: TSpeedButton;
    Image5: TImage;
    DSExits: TDataSource;
    GridExits: TSMDBGrid;
    procedure BtnCancelClick(Sender: TObject);
    procedure GridExitsKeyPress(Sender: TObject; var Key: Char);
    procedure BtnDeleteClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure BtnAddClick(Sender: TObject);
    procedure GridExitsDblClick(Sender: TObject);
    procedure BtnRenameClick(Sender: TObject);
    procedure BtnJumpClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure BtnHelpClick(Sender: TObject);
    procedure CheckNWClick(Sender: TObject);
    procedure ColorGridChange(Sender: TObject);
    procedure EditTagChange(Sender: TObject);
    procedure BtnClear1Click(Sender: TObject);
    procedure BtnEditClick(Sender: TObject);
    procedure GridExitsCalcCellColors(Sender: TObject; Field: TField;
      State: TGridDrawState; Highlight: Boolean; AFont: TFont;
      ABrush: TBrush);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
    procedure SaveOptions;
    procedure UpdateStubs;
    procedure UpdateRoom;
    procedure UpdateStubEnabling;
    function FigureStubs: string;
  public
    { Public declarations }
    multiple, cancelling: boolean;
    function LoadOptions(map: string; x, y: integer; local: boolean): boolean;
    function FigureRoom: integer;
  end;

implementation

{$R *.DFM}

uses Map, DBClient, Variants;

var
  GX, GY: integer;
  GMAP: string;

procedure TFormRProperties.FormCreate(Sender: TObject);
begin
  cancelling:=false;
  multiple:=false;
  GX:=0; GY:=0; GMAP:='';
  with ImageNW.canvas do begin
    brush.color:=clsilver;
    floodfill(0,0,cllime,fssurface);
  end;
  with ImageN.canvas do begin
    brush.color:=clsilver;
    floodfill(0,0,clsilver,fssurface);
  end;
  with ImageNE.canvas do begin
    brush.color:=clsilver;
    floodfill(0,0,clsilver,fssurface);
  end;
  with ImageE.canvas do begin
    brush.color:=clsilver;
    floodfill(0,0,clsilver,fssurface);
  end;
  with ImageSE.canvas do begin
    brush.color:=clsilver;
    floodfill(0,0,clsilver,fssurface);
  end;
  with ImageS.canvas do begin
    brush.color:=clsilver;
    floodfill(0,0,clsilver,fssurface);
  end;
  with ImageSW.canvas do begin
    brush.color:=clsilver;
    floodfill(0,0,clsilver,fssurface);
  end;
  with ImageW.canvas do begin
    brush.color:=clsilver;
    floodfill(0,0,clsilver,fssurface);
  end;
end;

function TFormRProperties.LoadOptions(map: string; x, y: integer; local: boolean): boolean;
const
   EXIT_FILTER = '(Map1 = %d) and (X = %d) and (Y = %d)';
var
  didfind: boolean;
  oldx, oldy: integer;
  oldmap, stubs: string;
begin
  result:=false;
  oldx:=GX; oldy:=GY; oldmap:=GMAP;
  with Datawell.TableRooms do begin
    filter := format('map_id = %d', [FormMap.GetMapId(map)]);
    if not Locate('x;y', VarArrayOf([x, y]), []) then begin
      filter := format('map_id = %d', [FormMap.GetMapId(oldMap)]);
      if local then
        Locate('x;y', VarArrayOf([oldx, oldy]), []);
      exit;
    end;
  end;
  result := true;
  didfind := not Datawell.TableRooms.FieldByName('Memo').IsNull;
  EditLabel.text:=Datawell.TableRooms.fieldbyname('Label').asstring;
  EditTag.text:=Datawell.TableRooms.fieldbyname('Tag').asstring;
  EditWarn.text:=Datawell.TableRooms.fieldbyname('Warn').asstring;
  stubs:=Datawell.TableRooms.fieldbyname('Stubs').asstring;
  EditNotes.lines.clear;
  if didfind then
    EditNotes.lines.assign(Datawell.TableRooms.fieldbyname('Memo'));
  with Datawell.TableExits do begin
    filter := format(EXIT_FILTER, [FormMap.GetMapID(map), x, y]);
    filtered := true;
    active:=true;
    first;
  end;
  GX:=x; GY:=y; GMAP:=map;
  EditX.text:=inttostr(x);
  EditY.text:=inttostr(y);
  EditMap.text:=map;
  CheckNW.checked:=false;
  CheckN.checked:=false;
  CheckNE.checked:=false;
  CheckE.checked:=false;
  CheckSE.checked:=false;
  CheckS.checked:=false;
  CheckSW.checked:=false;
  CheckW.checked:=false;
  Check5.checked:=false;
  if pos('7',stubs) > 0 then CheckNW.checked:=true;
  if pos('8',stubs) > 0 then CheckN.checked:=true;
  if pos('9',stubs) > 0 then CheckNE.checked:=true;
  if pos('6',stubs) > 0 then CheckE.checked:=true;
  if pos('3',stubs) > 0 then CheckSE.checked:=true;
  if pos('2',stubs) > 0 then CheckS.checked:=true;
  if pos('1',stubs) > 0 then CheckSW.checked:=true;
  if pos('4',stubs) > 0 then CheckW.checked:=true;
  if pos('5',stubs) > 0 then Check5.checked:=true;
  UpdateStubs;
  ColorGrid.foregroundindex:=Datawell.ColorFigure(Datawell.TableRooms.fieldbyname('Color').asinteger);
  ColorGrid.backgroundindex:=Datawell.ColorFigure(Datawell.TableRooms.fieldbyname('BColor').asinteger);
  case Datawell.TableRooms.fieldbyname('Kind').asinteger of
    0: rrr0.down:=true;
    1: rrr1.down:=true;
    2: rrr2.down:=true;
    3: rrr3.down:=true;
    4: rrr4.down:=true;
    5: rrr5.down:=true;
  end;
  UpdateRoom;
  UpdateStubEnabling;
end;

procedure TFormRProperties.SaveOptions;
var
  didfind: boolean;
begin
  EditWarn.text:=Datawell.CheckAllSpaces(EditWarn.text);
  EditLabel.text:=Datawell.CheckAllSpaces(EditLabel.text);
  if multiple then exit;
  didfind:=false;
  with Datawell.TableRooms do begin
    first;
    while not EOF do begin
      if (Datawell.TableRooms.fieldbyname('X').asinteger = GX) and
        (Datawell.TableRooms.fieldbyname('Y').asinteger = GY) then begin
        didfind:=true;
        break;
      end;
      next;
    end;
    if not didfind then exit;
    edit;
    Datawell.TableRooms.fieldbyname('Kind').asinteger:=FigureRoom;
    Datawell.TableRooms.fieldbyname('Label').asstring:=EditLabel.text;
    Datawell.TableRooms.fieldbyname('Tag').asstring:=EditTag.text;
    Datawell.TableRooms.fieldbyname('Warn').asstring:=EditWarn.text;
    Datawell.TableRooms.fieldbyname('Stubs').asstring:=FigureStubs;
    Datawell.TableRooms.fieldbyname('Color').asinteger:=ShapeRoom.pen.color;
    Datawell.TableRooms.fieldbyname('BColor').asinteger:=ShapeRoom.brush.color;
    Datawell.TableRooms.fieldbyname('Memo').assign(EditNotes.lines);
    post;
  end;
end;

procedure TFormRProperties.BtnCancelClick(Sender: TObject);
begin
  if TBitBtn(sender).tag = 1 then SaveOptions else cancelling:=true;
  close;
end;

procedure TFormRProperties.GridExitsDblClick(Sender: TObject);
begin
  if BtnEdit.enabled then BtnEdit.click;
end;

procedure TFormRProperties.GridExitsKeyPress(Sender: TObject;
  var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    BtnEdit.click;
  end;
end;

procedure TFormRProperties.BtnDeleteClick(Sender: TObject);
begin
  if Datawell.TableExits.fieldbyname('FExit').asstring = '' then begin
    messagedlg('No exit to delete',mtinformation,[mbok],0);
    exit;
  end;
  if not Datawell.TableExits.EOF then Datawell.TableExits.delete;
  UpdateStubEnabling;
end;

procedure TFormRProperties.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key = VK_DELETE then begin
    if activecontrol = GridExits then begin
      BtnDelete.click;
      key:=0;
    end;
  end;
end;

procedure TFormRProperties.BtnRenameClick(Sender: TObject);
var
  form: TFormRenameExit;
  ex: string;
begin
  if Datawell.TableExits.fieldbyname('FExit').asstring = '' then begin
    messagedlg('No exit to rename',mtinformation,[mbok],0);
    exit;
  end;
  form:=TFormRenameExit.create(self);
  form.ex:=Datawell.TableExits.fieldbyname('FExit').asstring;
  form.map:=datawell.TableMaps.Lookup('map_id', datawell.TableExits.FieldByName('Map1').Value, 'Map');
  form.x:=inttostr(Datawell.TableExits.fieldbyname('X').asinteger);
  form.y:=inttostr(Datawell.TableExits.fieldbyname('Y').asinteger);
  form.showmodal;
  ex:=form.ex;
  form.free;
  if ex = '' then exit;
  Datawell.TableExits.edit;
  Datawell.TableExits.fieldbyname('FExit').asstring:=ex;
  Datawell.TableExits.post;
  BtnSave.setfocus;
  UpdateStubEnabling;
end;

procedure TFormRProperties.BtnJumpClick(Sender: TObject);
var
  tryx, tryy: integer;
  trymap: string;
begin
  if Datawell.TableExits.fieldbyname('FExit').asstring = '' then begin
    messagedlg('No exit to jump',mtinformation,[mbok],0);
    exit;
  end;
  SaveOptions;
  trymap:=Datawell.TableExits.fieldbyname('destmap').asstring;
  tryx:=Datawell.TableExits.fieldbyname('X2').asinteger;
  tryy:=Datawell.TableExits.fieldbyname('Y2').asinteger;
  if LoadOptions(trymap,tryx,tryy,true) then exit;
  messagedlg('There is no room at ('+inttostr(tryx)+','+inttostr(tryy)+') ['+
    trymap+']',mtinformation,[mbok],0);
end;

procedure TFormRProperties.FormMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  Status.caption:=' '+(sender as TControl).hint;
end;

procedure TFormRProperties.FormShow(Sender: TObject);
begin
  FocusControl(GridExits);
end;

procedure TFormRProperties.BtnHelpClick(Sender: TObject);
begin
  Datawell.Help('room_properties');
end;

procedure TFormRProperties.CheckNWClick(Sender: TObject);
begin
  UpdateStubs;
end;

procedure TFormRProperties.UpdateStubs;
begin
  ImageNW.visible:=CheckNW.checked;
  ImageN.visible:=CheckN.checked;
  ImageNE.visible:=CheckNE.checked;
  ImageE.visible:=CheckE.checked;
  ImageSE.visible:=CheckSE.checked;
  ImageS.visible:=CheckS.checked;
  ImageSW.visible:=CheckSW.checked;
  ImageW.visible:=CheckW.checked;
  Shape5.visible:=Check5.checked;
end;

function TFormRProperties.FigureRoom: integer;
begin
  result:=-1;
  if rrr0.down then result:=0
  else if rrr1.down then result:=1
  else if rrr2.down then result:=2
  else if rrr3.down then result:=3
  else if rrr4.down then result:=4
  else if rrr5.down then result:=5;
end;

procedure TFormRProperties.ColorGridChange(Sender: TObject);
begin
  UpdateRoom;
end;

procedure TFormRProperties.UpdateRoom;
var img: TImage;
begin
  ShapeRoom.pen.color:=Datawell.FigureColor(ColorGrid.foregroundindex);
  ShapeRoom.brush.color:=Datawell.FigureColor(ColorGrid.backgroundindex);
  LabelTag.font.color:=ShapeRoom.pen.color;
  LabelTag.color:=ShapeRoom.brush.color;
  if rrr0.down then begin
    ImageStyle.hide;
    LabelTag.show;
  end
  else begin
    if rrr1.down then img:=Image1
    else if rrr2.down then img:=Image2
    else if rrr3.down then img:=Image3
    else if rrr4.down then img:=Image4
    else if rrr5.down then img:=Image5
    else raise Exception.Create('TFormRProperties.UpdateRoom: No image button is pressed.');
    with ImageStyle.canvas do begin
      brush.color:=ShapeRoom.brush.color;
      BrushCopy(Bounds(0,0,29,29),img.picture.bitmap,Bounds(0,0,29,29),clwhite);
      brush.color:=ShapeRoom.pen.color;
      floodfill(15,15,clblack,fssurface);
    end;
    LabelTag.hide;
    ImageStyle.show;
  end;
end;

procedure TFormRProperties.EditTagChange(Sender: TObject);
begin
  LabelTag.caption:=EditTag.text;
end;

function TFormRProperties.FigureStubs: string;
begin
  result:='';
  if CheckNW.checked then result:=result+'7';
  if CheckN.checked then result:=result+'8';
  if CheckNE.checked then result:=result+'9';
  if CheckE.checked then result:=result+'6';
  if CheckSE.checked then result:=result+'3';
  if CheckS.checked then result:=result+'2';
  if CheckSW.checked then result:=result+'1';
  if CheckW.checked then result:=result+'4';
  if Check5.checked then result:=result+'5';
end;

procedure TFormRProperties.UpdateStubEnabling;
const
  FILTER_STR = '(Map1 = %d) and (X = %d) and (Y = %d)';
var
  table: TClientDataset;
  d1, d2, d3, d4, d6, d7, d8, d9: boolean;
  ex: string;
begin
  d1:=true; d2:=true; d3:=true; d4:=true;
  d6:=true; d7:=true; d8:=true; d9:=true;
  table:=TClientDataset.create(self);
  with table do begin
    CloneCursor(Datawell.TableExits, false);
    filter := format(FILTER_STR, [FormMap.GetMapID(GMAP), GX, GY]);
    filtered := true;
    active:=true;
    first;
    while not EOF do begin
      ex:=fieldbyname('FExit').asstring;
      if (ex = Datawell.Dir('n')) or (ex = Datawell.Dir('north')) then d8:=false;
      if (ex = Datawell.Dir('ne')) or (ex = Datawell.Dir('northeast')) then d9:=false;
      if (ex = Datawell.Dir('e')) or (ex = Datawell.Dir('east')) then d6:=false;
      if (ex = Datawell.Dir('se')) or (ex = Datawell.Dir('southeast')) then d3:=false;
      if (ex = Datawell.Dir('s')) or (ex = Datawell.Dir('south')) then d2:=false;
      if (ex = Datawell.Dir('sw')) or (ex = Datawell.Dir('southwest')) then d1:=false;
      if (ex = Datawell.Dir('w')) or (ex = Datawell.Dir('west')) then d4:=false;
      if (ex = Datawell.Dir('nw')) or (ex = Datawell.Dir('northwest')) then d7:=false;
      next;
    end;
  end;
  table.free;
  CheckN.enabled:=d8;
  CheckNE.enabled:=d9;
  CheckE.enabled:=d6;
  CheckSE.enabled:=d3;
  CheckS.enabled:=d2;
  CheckSW.enabled:=d1;
  CheckW.enabled:=d4;
  CheckNW.enabled:=d7;
  if not CheckN.enabled then CheckN.checked:=false;
  if not CheckNE.enabled then CheckNE.checked:=false;
  if not CheckE.enabled then CheckE.checked:=false;
  if not CheckSE.enabled then CheckSE.checked:=false;
  if not CheckS.enabled then CheckS.checked:=false;
  if not CheckSW.enabled then CheckSW.checked:=false;
  if not CheckW.enabled then CheckW.checked:=false;
  if not CheckNW.enabled then CheckNW.checked:=false;
end;

procedure TFormRProperties.BtnClear1Click(Sender: TObject);
begin
  EditWarn.text:='';
end;

procedure TFormRProperties.BtnAddClick(Sender: TObject);
var
  form: TFormConfirmLink;
  x1, y1, x2, y2, r1, r2, r3, r4, r5, r6, lcol: integer;
  s1, s2, s3, s4, s5, s6, link1, link2, map1, map2, p1, p2, p3, p4, ss1, ss2: string;
  doroom1, doroom2, linked: boolean;
begin
  x1:=GX; y1:=GY;
  x2:=x1; y2:=y1;
  map1:=EditMap.text;
  map2:=map1;
  link1:='';
  link2:='';
  doroom1:=true;
  doroom2:=true;
  //Set properties of endpoint rooms
  r1:=FormMap.FigureRoom;
  r2:=Datawell.FigureColor(FormMap.ColorGrid.foregroundindex);
  r3:=Datawell.FigureColor(FormMap.ColorGrid.backgroundindex);
  s1:=''; s2:=''; s3:=''; ss1:='';
  FormMap.RoomProperties(map1,x1,y1,r1,r2,r3,s1,s2,s3,ss1);
  s1:=EditLabel.text; s2:=EditTag.text; s3:=EditWarn.text; ss1:=FigureStubs;
  r4:=FormMap.FigureRoom;
  r5:=Datawell.FigureColor(FormMap.ColorGrid.foregroundindex);
  r6:=Datawell.FigureColor(FormMap.ColorGrid.backgroundindex);
  s4:=''; s5:=''; s6:=''; ss2:='';
  p1:=''; p2:=''; p3:=''; p4:='';
  form:=TFormConfirmLink.create(self);
  try
    case r1 of
      0: form.b0.down:=true;
      1: form.b1.down:=true;
      2: form.b2.down:=true;
      3: form.b3.down:=true;
      4: form.b4.down:=true;
    end;
    case r4 of
      0: form.c0.down:=true;
      1: form.c1.down:=true;
      2: form.c2.down:=true;
      3: form.c3.down:=true;
      4: form.c4.down:=true;
    end;
    form.ColorLink.foregroundindex:=FormMap.ColorGrid.foregroundindex;
    form.Colors1.foregroundindex:=Datawell.ColorFigure(r2);
    form.Colors1.backgroundindex:=Datawell.ColorFigure(r3);
    form.Colors2.foregroundindex:=Datawell.ColorFigure(r5);
    form.Colors2.backgroundindex:=Datawell.ColorFigure(r6);
    form.Edit1.text:=link1;
    form.Edit2.text:=link2;
    form.EditMap1.ItemIndex := form.EditMap1.Items.IndexOf(map1);
    form.X1.value:=x1;
    form.Y1.value:=y1;
    form.EditMap2.ItemIndex := form.EditMap2.Items.IndexOf(map2);
    form.X2.value:=x2;
    form.Y2.value:=y2;
    form.EditPre1.text:=p1;
    form.EditPost1.text:=p2;
    form.EditPre2.text:=p3;
    form.EditPost2.text:=p4;
    form.UpdateEnabling;
    form.showmodal;
    map1:=form.EditMap1.text;
    map2:=form.EditMap2.text;
    link1:=form.Edit1.text;
    link2:=form.Edit2.text;
    p1:=form.EditPre1.text;
    p2:=form.EditPost1.text;
    p3:=form.EditPre2.text;
    p4:=form.EditPost2.text;
    if form.CheckO.enabled then begin
      doroom1:=form.CheckO.checked;
      r1:=form.FigureRoom(1);
      r2:=Datawell.FigureColor(form.Colors1.ForegroundIndex);
      r3:=Datawell.FigureColor(form.Colors1.BackgroundIndex);
    end;
    if form.CheckD.enabled then begin
      doroom2:=form.CheckD.checked;
      r4:=form.FigureRoom(2);
      r5:=Datawell.FigureColor(form.Colors2.ForegroundIndex);
      r6:=Datawell.FigureColor(form.Colors2.BackgroundIndex);
    end;
    lcol:=Datawell.FigureColor(form.ColorLink.foregroundindex);
    x1:=trunc(form.X1.value);
    y1:=trunc(form.Y1.value);
    x2:=trunc(form.X2.value);
    y2:=trunc(form.Y2.value);
    linked:=form.linked;
  finally
    form.free;
  end;
  if not linked then exit;
  inc(FormMap.undoid);
  FormMap.overrideroom:=true;
  if doroom1 then FormMap.CreateRoom(map1,x1,y1,r1,r2,r3,s1,s2,s3,ss1,true);
  FormMap.RoomProperties(map2,x2,y2,r1,r2,r3,s4,s5,s6,ss2);
  if doroom2 then FormMap.CreateRoom(map2,x2,y2,r4,r5,r6,s4,s5,s6,ss2,true);
  FormMap.CreateLinkExits(map1,map2,link1,link2,p1,p2,p3,p4,x1,y1,x2,y2,lcol,true);
  FormMap.overrideroom:=false;
  //Check for attic/basement
  FormMap.CheckBasement(map1,map2,link1,link2);
  FormMap.DrawMap(false);
  if (link1 = Datawell.Dir('n')) or (link1 = Datawell.Dir('north')) then CheckN.checked:=false;
  if (link1 = Datawell.Dir('ne')) or (link1 = Datawell.Dir('northeast')) then CheckNE.checked:=false;
  if (link1 = Datawell.Dir('e')) or (link1 = Datawell.Dir('east')) then CheckE.checked:=false;
  if (link1 = Datawell.Dir('se')) or (link1 = Datawell.Dir('southeast')) then CheckSE.checked:=false;
  if (link1 = Datawell.Dir('s')) or (link1 = Datawell.Dir('south')) then CheckS.checked:=false;
  if (link1 = Datawell.Dir('sw')) or (link1 = Datawell.Dir('southwest')) then CheckSW.checked:=false;
  if (link1 = Datawell.Dir('w')) or (link1 = Datawell.Dir('west')) then CheckW.checked:=false;
  if (link1 = Datawell.Dir('nw')) or (link1 = Datawell.Dir('northwest')) then CheckNW.checked:=false;
  ColorGrid.foregroundindex:=Datawell.ColorFigure(r2);
  ColorGrid.backgroundindex:=Datawell.ColorFigure(r3);
  case r1 of
    0: rrr0.down:=true;
    1: rrr1.down:=true;
    2: rrr2.down:=true;
    3: rrr3.down:=true;
    4: rrr4.down:=true;
    5: rrr5.down:=true;
  end;
  UpdateRoom;
  UpdateStubEnabling;
end;

procedure TFormRProperties.BtnEditClick(Sender: TObject);
var
  form: TFormRoomExit;
  table: TClientDataset;
  didfind: boolean;
begin
  if Datawell.TableExits.fieldbyname('FExit').asstring = '' then begin
    messagedlg('No exit to edit',mtinformation,[mbok],0);
    exit;
  end;
  form:=TFormRoomExit.create(self);
  with Datawell.TableExits do begin
    form.EditExit.text:=fieldbyname('FExit').asstring;
    form.EditPreAction.text:=fieldbyname('PreAction').asstring;
    form.EditPostAction.text:=fieldbyname('PostAction').asstring;
    form.EditPostAction.text:=fieldbyname('PostAction').asstring;
    form.ColorGrid.ForegroundIndex:=Datawell.ColorFigure(fieldbyname('Color').asinteger);
    form.showmodal;
    if form.linked then begin
      edit;
      fieldbyname('FExit').asstring:=form.EditExit.text;
      fieldbyname('PreAction').asstring:=form.EditPreAction.text;
      fieldbyname('PostAction').asstring:=form.EditPostAction.text;
      fieldbyname('Color').asinteger:=Datawell.FigureColor(form.ColorGrid.ForegroundIndex);
      post;
      //Check opposite exit for coloring
      table:=TClientDataset.create(self);
      table.CloneCursor(Datawell.TableExits, true);
      table.active:=true;
      didfind:=false;
      table.first;
      while not table.EOF do begin
        if (table.fieldbyname('Map1').AsInteger = fieldbyname('Map2').AsInteger) and
          (table.fieldbyname('X').asinteger = fieldbyname('X2').asinteger) and
          (table.fieldbyname('Y').asinteger = fieldbyname('Y2').asinteger) and
          (table.fieldbyname('Map2').AsInteger = fieldbyname('Map1').AsInteger) and
          (table.fieldbyname('X2').asinteger = fieldbyname('X').asinteger) and
          (table.fieldbyname('Y2').asinteger = fieldbyname('Y').asinteger) then begin
          didfind:=true;
          break;
        end;
        table.next;
      end;
      if didfind then begin
        table.edit;
        table.fieldbyname('Color').asinteger:=fieldbyname('Color').asinteger;
        table.post;
      end;
      table.free;
    end;
  end;
  form.free;
  GridExits.refresh;
end;

procedure TFormRProperties.GridExitsCalcCellColors(Sender: TObject;
  Field: TField; State: TGridDrawState; Highlight: Boolean; AFont: TFont;
  ABrush: TBrush);
begin
  if Field.fieldname = 'Exit' then AFont.style:=[fsbold];
  if Field.fieldname = 'Order' then begin
    AFont.color:=Datawell.TableExits.fieldbyname('Color').asinteger;
    ABrush.color:=Datawell.TableExits.fieldbyname('Color').asinteger;
  end;
end;

end.
